<template>
  <div class="login-container">
    <div class="login-box">
      <img class="login-banner" src="@/assets/images/login-banner.png">
      <div class="welcome">
        <p class="title">欢迎登录</p>
        <p class="content">义城通商家中心</p>
      </div>
      <div class="login-form">
        <p class="login-title">义城通商家中心</p>
        <el-tabs v-if="loginType === '1' || loginType === '2'" v-model="loginType" class="login-tabs" @tab-click="changeLoginType">
          <el-tab-pane label="账号登录" name="1" />
          <el-tab-pane label="手机号登录" name="2" />
        </el-tabs>
        <el-form
          v-if="loginType === '1'"
          :key="loginType"
          ref="accountForm"
          :model="accountForm"
          :rules="accountRules"
          label-width="80px"
          label-position="left"
          class="account-form"
        >
          <el-form-item label="账号：" prop="account">
            <el-input
              v-model="accountForm.account"
              class="form-input"
              placeholder="请输入账号"
              type="text"
              tabindex="1"
            />
          </el-form-item>

          <el-form-item label="密码：" prop="password">
            <el-input
              v-model="accountForm.password"
              class="form-input"
              type="password"
              placeholder="请输入密码"
              tabindex="2"
            />
          </el-form-item>
          <p>
            <el-button :loading="loading" class="login-btn" type="primary" @click.native.prevent="handleLogin">登录
            </el-button>
          </p>
          <p>
            <el-button class="login-btn" @click.native.prevent="gotoRegister">注册</el-button>
          </p>
        </el-form>
        <el-form
          v-if="loginType === '2'"
          :key="loginType"
          ref="mobileForm"
          :model="mobileForm"
          :rules="mobileRules"
          label-width="80px"
          label-position="left"
          class="mobile-form"
        >
          <el-form-item label="手机号：" prop="mobile">
            <el-input
              v-model="mobileForm.mobile"
              v-int
              class="form-input"
              placeholder="请输入手机号"
              type="text"
              tabindex="1"
              maxlength="11"
            />
          </el-form-item>

          <el-form-item label="验证码：" prop="smsCode">
            <el-input
              v-model="mobileForm.smsCode"
              class="form-input"
              type="text"
              placeholder="请输入验证码"
              tabindex="2"
            >
              <span v-if="showTime" slot="append" class="send-code">{{ durationTime }}s</span>
              <el-button v-else slot="append" class="send-code" @click="getLoginCode">获取验证码</el-button>
            </el-input>

          </el-form-item>
          <p>
            <el-button :loading="loading" class="login-btn" type="primary" @click.native.prevent="handleLogin">登录
            </el-button>
          </p>
          <p>
            <el-button class="login-btn" @click.native.prevent="gotoRegister">注册</el-button>
          </p>
        </el-form>
        <el-form
          v-if="loginType === '3'"
          :key="loginType"
          ref="registerForm"
          :model="registerForm"
          :rules="registerRules"
          label-width="100px"
          label-position="left"
          class="register-form"
        >
          <el-form-item label="账号：" prop="account">
            <el-input
              v-model="registerForm.account"
              class="form-input"
              placeholder="请输入账号"
              type="text"
              tabindex="1"
            />
          </el-form-item>
          <el-form-item label="密码：" prop="password">
            <el-input
              v-model="registerForm.password"
              class="form-input"
              placeholder="请输入密码"
              type="password"
              tabindex="2"
            />
          </el-form-item>
          <el-form-item label="确认密码：" prop="password2">
            <el-input
              v-model="registerForm.password2"
              class="form-input"
              placeholder="请输入密码"
              type="password"
              tabindex="3"
            />
          </el-form-item>
          <el-form-item label="手机号：" prop="mobile">
            <el-input
              v-model="registerForm.mobile"
              v-int
              class="form-input"
              placeholder="请输入密码"
              type="text"
              tabindex="4"
              maxlength="11"
            />
          </el-form-item>
          <el-form-item label="验证码：" prop="smsCode">
            <el-input
              v-model="registerForm.smsCode"
              class="form-input"
              type="text"
              placeholder="请输入验证码"
              tabindex="5"
            >
              <span v-if="showTime" slot="append" class="send-code">{{ durationTime }}s</span>
              <el-button v-else slot="append" class="send-code" @click="getRegisterCode">获取验证码</el-button>
            </el-input>
          </el-form-item>
          <el-form-item prop="checked">
            <el-checkbox v-model="registerForm.checked" />
            <span class="agree-tip">阅读并同意义城通的<a target="_blank" class="agree-text" href="#">隐私协议</a>和<a target="_blank" class="agree-text" href="#">用户协议</a></span>
          </el-form-item>
          <p>
            <el-button :loading="loading" class="login-btn" type="primary" @click.native.prevent="handleRegister">注册</el-button>
          </p>
        </el-form>
      </div>
    </div>
    <img class="login-bg" src="@/assets/images/login-bg.png">
  </div>
</template>
<script>
import { accountLogin, mobileLogin, sendSms, register } from '@/api/login'
import { mobileReg } from '@/utils/const'
export default {
  name: 'Login',
  data() {
    // const validatePassword = (rule, value, callback) => {
    //   if (value.length < 6) {
    //     callback(new Error('The password can not be less than 6 digits'))
    //   } else {
    //     callback()
    //   }
    // }
    return {
      loginType: '1', // 1：账号登录 2：手机号登录  3：注册
      accountForm: {
        account: 'zhou',
        password: '123456'
      },
      mobileForm: {
        mobile: '13387519403',
        smsCode: '123456'
      },
      registerForm: {
        account: 'zhou',
        password: '123456',
        password2: '123456',
        mobile: '13387519403',
        smsCode: '123456',
        checked: ''
      },
      accountRules: {
        account: [{ required: true, trigger: 'blur', message: '请输入账号' }],
        password: [{ required: true, trigger: 'blur', message: '请输入密码' }]
      },
      mobileRules: {
        mobile: [
          { required: true, trigger: 'blur', message: '请输入手机号' },
          { pattern: mobileReg, message: '请输入正确的手机号', trigger: 'blur' }
        ],
        smsCode: [{ required: true, trigger: 'blur', message: '请输入验证码' }]
      },
      registerRules: {
        account: [{ required: true, trigger: 'blur', message: '请输入账号' }],
        password: [{ required: true, trigger: 'blur', message: '请输入密码' }],
        password2: [{ required: true, trigger: 'blur', message: '请输入密码' }],
        mobile: [
          { required: true, trigger: 'blur', message: '请输入手机号' },
          { pattern: mobileReg, message: '请输入正确的手机号', trigger: 'blur' }
        ],
        smsCode: [{ required: true, trigger: 'blur', message: '请输入验证码' }],
        checked: [{ required: true, trigger: 'blur', message: '请阅读并同意' }]
      },
      timer: null,
      showTime: false,
      durationTime: 59,
      loading: false,
      redirect: ''
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {

    // 切换登录方式
    changeLoginType() {
      this.showTime = false
      this.durationTime = 59
    },

    // 去注册
    gotoRegister() {
      this.loginType = '3'
      this.showTime = false
      this.durationTime = 59
    },

    // 获取登录验证码
    getLoginCode() {
      const data = {
        mobile: this.mobileForm.mobile,
        smsType: 'loginBucket'
      }
      sendSms(data).then(() => {
        this.timeStart()
      }).catch(() => {})
    },

    // 获取注册验证码
    getRegisterCode() {
      const data = {
        mobile: this.registerForm.mobile,
        smsType: 'registerBucket'
      }
      sendSms(data).then(() => {
        this.timeStart()
      }).catch(() => {})
    },

    // 倒计时
    timeStart() {
      this.showTime = true
      this.timer = setInterval(() => {
        while (this.durationTime === 1) {
          this.showTime = false
          this.durationTime = 59
          clearInterval(this.timer)
          return
        }
        this.durationTime--
      }, 1000)
    },

    // 登录
    async handleLogin() {
      let $form
      let data = {}
      let loginApi
      if (this.loginType === '1') {
        $form = this.$refs.accountForm
        data = this.accountForm
        loginApi = accountLogin
      } else {
        $form = this.$refs.mobileForm
        data = this.mobileForm
        loginApi = mobileLogin
      }
      $form.validate(async(valid) => {
        if (valid) {
          try {
            this.loading = true
            await this.$store.dispatch('user/login', { data, loginApi })
            const enterpriseList = await this.$store.dispatch('user/getEnterprisesList')
            if (enterpriseList.length) {
              await this.$store.dispatch('user/getSiteList', { enterpriseId: enterpriseList[0].id })
              await this.$store.dispatch('user/getUserInfo')
            }
            await this.$router.push({ path: this.redirect || '/' })
            this.loading = false
          } catch (e) {
            this.loading = false
          }
        } else {
          return false
        }
      })
    },

    // 注册
    handleRegister() {
      const data = this.registerForm
      this.$refs.registerForm.validate(valid => {
        if (valid) {
          this.loading = true
          register(data).then(() => {
            this.loading = false
            this.$router.push({ path: this.redirect || '/' })
          }).catch(() => {
            this.loading = false
          })
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
@import "@/styles/variables.scss";
.login-container {
  position: relative;
  width: 100vw;
  height: 100vh;
  overflow: hidden;
  background: $bg-color ;

  .login-box{
    position: absolute;
    display: flex;
    height: 430px;
    align-items: center;
    justify-content: left;
    border-radius: 0 8px 8px 0;
    top: 50%;
    right: 50px;
    transform: translateY(-50%);
    overflow: hidden;
    z-index: 10;
  }
  .login-banner{
    width: 500px;
    height: auto;
  }

  .welcome{
    position: absolute;
    top:100px;
    left: 50px;
    color: #fff;
    .title{
      font-size: 16px;
      margin-bottom: 10px;
    }
    .content{
      font-size: 24px;
    }
  }

  .login-form {
    width: 470px;
    height: 100%;
    background: #fff;
    box-shadow: 0 0 20px 0 rgba(0, 13, 88, .1);
    padding: 20px 30px;

    .login-title {
      font-size: 28px;
      color: $primary;
      margin-bottom: 20px;
      text-align: center;
      font-weight: bold;
    }

    .go-back{
      font-size: 14px;
      color: $primary;
      margin-bottom: 15px;
    }

    .login-tabs{
      margin-bottom: 20px;
    }

    .form-input{
      width: 100%;
      margin-bottom: 10px;
    }

    .register-form .form-input{
      margin-bottom: 0;
    }

    .send-code{
      color: $primary;
    }

    .agree-tip{
      margin-left: 5px;
    }

    .agree-text{
      color: $primary;
      padding: 0 3px;
    }

    ::v-deep {
      .account-form, .mobile-form{
        .el-input__inner,.el-form-item__label{
          height: 40px;
          line-height: 40px;
        }
      }
    }

    .login-btn {
      display: inline-block;
      width: 100%;
      height: 40px;
      font-size: 18px;
      margin-bottom: 15px;
    }
  }

  .login-bg{
    position: absolute;
    width: 100vw;
    bottom: 0;
    left: 0;
    z-index: 1;
  }
}
</style>
